package com.wlyuan.open.domain.source.dispatcher.conversion;

import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.dto.tenant.OpenDriverTenantSyncDTO;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

/**
 * @author lijing
 * @description
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class OpenDispatcherDriverTenantConversion implements OpenDispatcherConversion<OpenDriverTenantSyncDTO> {
    private final BaseOpenDispatcherConversion baseOpenDispatcherConversion;

    @Override
    public void convert(OpenDriverTenantSyncDTO source) {
        //转换邀请者租户 和 邀请者租户ID （增量同步时）
        var inviterTenantId = source.getInviterTenantId();
        if (LongUtils.notNullLong(inviterTenantId)) {
            var tenantId = baseOpenDispatcherConversion.tenant(inviterTenantId);
            AssertUtils.assertNotNull(tenantId, new ValueError("OpenDriverTenantSyncDTO 邀请租户不存在:" + inviterTenantId), source);
            source.setInviterTenantId(tenantId);
        }
        var inviterUserId = source.getInviterUserId();
        if (LongUtils.notNullLong(inviterUserId)) {
            var userId = baseOpenDispatcherConversion.user(inviterUserId);
            AssertUtils.assertNotNull(userId, new ValueError("OpenDriverTenantSyncDTO 邀请用户不存在:" + inviterUserId), source);
            source.setInviterUserId(userId);
        }

        //创建人
        var createBy = source.getCreateBy();
        if (LongUtils.notNullLong(createBy)) {
            Long user = baseOpenDispatcherConversion.user(createBy);
            AssertUtils.assertNotNull(user, new ValueError("OpenDriverTenantSyncDTO 创建人不存在:" + createBy), source);
            source.setCreateBy(user);
        }
        //修改人
        var updateBy = source.getUpdateBy();
        if (LongUtils.notNullLong(updateBy)) {
            Long user = baseOpenDispatcherConversion.user(updateBy);
            AssertUtils.assertNotNull(user, new ValueError("OpenDriverTenantSyncDTO 修改人不存在:" + updateBy), source);
            source.setUpdateBy(user);
        }

    }
}
